home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk10.zip / DECL.AWK < prev    next >
Text File  |  1991-10-05  |  3KB  |  144 lines

  1.  
  2. # parse a C declaration by recursive descent
  3. # based on a C program in KR ANSI edition
  4. #
  5. # run on a C file it finds the declarations
  6. #
  7. # restrictions: one declaration per line
  8. #               doesn't understand struct {...}
  9. #               makes assumptions about type names
  10. #
  11. #
  12. #  some awks need double escapes on strings used as
  13. #  regular expressions.  If not run on mawk, use gdecl.awk
  14.  
  15.  
  16. ################################################
  17. #   lexical scanner -- gobble()
  18. #   input : string s -- treated as a regular expression
  19. #   gobble eats SPACE, then eats longest match of s off front
  20. #   of global variable line.
  21. #   Cuts the matched part off of line
  22. #
  23.  
  24.  
  25. function gobble(s,  x)  
  26. {
  27.   sub( /^ /, "", line)  # eat SPACE if any
  28.  
  29.   # surround s with parenthesis to make sure ^ acts on the
  30.   # whole thing
  31.  
  32.   match(line, "^" "(" s ")")
  33.   x = substr(line, 1, RLENGTH)
  34.   line = substr(line, RLENGTH+1)
  35.   return x 
  36. }
  37.  
  38.  
  39. function ptr_to(n,  x)  # print "pointer to" , n times
  40. { n = int(n)
  41.   if ( n <= 0 )  return ""
  42.   x = "pointer to" ; n--
  43.   while ( n-- )  x = x " pointer to"
  44.   return x
  45. }
  46.  
  47.  
  48. #recursively get a decl
  49. # returns an english description of the declaration or
  50. # "" if not a C declaration.
  51.  
  52. function  decl(   x, t, ptr_part)
  53. {
  54.  
  55.   x = gobble("[* ]+")   # get list of *** ...
  56.   gsub(/ /, "", x)   # remove all SPACES
  57.   ptr_part = ptr_to( length(x) )
  58.  
  59.   # We expect to see either an identifier or '('
  60.   #
  61.  
  62.   if ( gobble("\(") )
  63.   { 
  64.     # this is the recursive descent part
  65.     # we expect to match a declaration and closing ')'
  66.     # If not return "" to indicate  failure
  67.  
  68.       if ( (x = decl()) == "" || gobble( "\)" ) == "" ) return ""
  69.  
  70.   }
  71.   else  #  expecting an identifier
  72.   {
  73.     if ( (x = gobble(id)) == "" )  return ""
  74.     x = x ":"
  75.   }
  76.  
  77.   # finally look for ()
  78.   # or  [ opt_size ]
  79.  
  80.   while ( 1 )
  81.      if ( gobble( funct_mark ) )  x = x " function returning"
  82.      else
  83.      if ( t = gobble( array_mark ) )
  84.      { gsub(/ /, "", t)
  85.        x = x " array" t " of"
  86.      }
  87.      else  break
  88.  
  89.  
  90.    x = x " "  ptr_part
  91.    return x
  92. }
  93.     
  94.  
  95. BEGIN { id = "[_A-Za-z][_A-Za-z0-9]*" 
  96.         funct_mark = "\([ \t]*\)"
  97.     array_mark = "\[[ \t]*[_A-Za-z0-9]*[ \t]*\]"
  98.  
  99. # I've assumed types are keywords or all CAPS or end in _t
  100. # Other conventions could be added.
  101.  
  102.     type0 = "int|char|short|long|double|float|void" 
  103.     type1 = "[_A-Z][_A-Z0-9]*"  #  types are CAPS
  104.     type2 = "[_A-Za-z][_A-Za-z0-9]*_t"  # end in _t
  105.  
  106.     types = "(" type0 "|" type1 "|" type2 ")"
  107. }
  108.  
  109.  
  110. {   
  111.  
  112.     gsub( "/\*([^*]|\*[^/])*(\*/|$)" , " ") # remove comments
  113.     gsub( /[ \t]+/, " ")  # squeeze white space to a single space
  114.  
  115.  
  116.     line = $0
  117.  
  118.     scope = gobble( "extern|static" )
  119.  
  120.     if ( type = gobble("(struct|union|enum) ") )
  121.             type = type gobble(id)  #  get the tag
  122.     else
  123.     {
  124.  
  125.        type = gobble("(un)?signed ") gobble( types )
  126.  
  127.     }
  128.     
  129.     if ( ! type )  next
  130.     
  131.     if ( (x = decl()) && gobble( ";") )
  132.     {
  133.       x  =  x " " type
  134.       if ( scope )  x = x " (" scope ")"
  135.       gsub( /  +/, " ", x)  # 
  136.       print x
  137.     }
  138.  
  139. }
  140.  
  141.  
  142.  
  143.  
  144.